package cn.xmirror.fsof;

import cn.xmirror.fsof.utils.MyReflectionUtils;
import javax.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.env.Environment;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.web.servlet.DispatcherServlet;

/**
 * <h1> Provider2App </h1>
 *
 * @author yuwen
 * @since 2022-07-08
 */
@SpringBootApplication
@ImportResource(value = {
        "classpath:spring/dubbo-demo-provider2.xml"
})
@ComponentScan(basePackages = "cn.xmirror")
@Slf4j
public class Provider2App {

    public static void main(String[] args) {
//        SpringApplication.run(ProviderApp.class, args);
//        new SpringApplicationBuilder(ProviderApp.class).web(WebApplicationType.NONE).run(args);
        ConfigurableApplicationContext applicationContext = SpringApplication.run(Provider2App.class, args);

        /**
         * 从spring上下文获取DispatcherServlet，对其进行处理
         */
        DispatcherServlet dispatcherServlet = applicationContext.getBean(DispatcherServlet.class);
        MockServletConfig myServletConfig = new MockServletConfig();
        MyReflectionUtils.setFieldValue(dispatcherServlet,"config",myServletConfig);

        /**
         * 初始化servlet
         */
        try {
            dispatcherServlet.init();
        } catch (ServletException e) {
            log.error("e:{}",e);
        }

        /**
         * 启动 netty 服务器
         */
        Environment environment = applicationContext.getBean(Environment.class);
        String port = environment.getProperty("server.port");

        new MyServer(Integer.valueOf(port)).run();
    }
}
